#!/usr/bin/env python
# -*- coding: utf-8 -*-

import gtk
import os, os.path

from modules import pad_notebook
from modules.pad_editor import Pad_editor
from common import file_fun
from common import file_dialog

class Pad_menubar(gtk.MenuBar):
    def __init__(self, mainwindow = gtk.Window(), accel_group = gtk.AccelGroup()):
        gtk.MenuBar.__init__(self)

        self.__mainwindow   = mainwindow
        self.__accel_group  = accel_group
        self.__item_file    = self.menu_file()
        self.__notebook     = None#pad_notebook.Pad_notebook()

        self.append(self.__item_file)

    def set_notebook(self, notebook):
        self.__notebook = notebook

    def set_mainwindow(self, mainwindow):
        self.__mainwindow = mainwindow

    def get_notebook_n_page(self):
        return self.__notebook.get_n_pages()

    def menu_file(self):
        """
        初始化文件菜单
        """
        item    = gtk.MenuItem('文件(_F)')
        menu    = gtk.Menu()
        item.set_submenu(menu)
        new     = gtk.MenuItem('新建(_N)')
        open    = gtk.MenuItem('打开(_O)')
        save    = gtk.MenuItem('保存(_S)')
        save_as = gtk.MenuItem('另存为(_A)')
        #open.set_sensitive(False)
        close   = gtk.MenuItem('关闭(_C)')
        exit    = gtk.MenuItem('退出(_X)')
        menu.append(new)
        menu.append(open)
        menu.append(save)
        menu.append(save_as)
        menu.append(close)
        menu.append(gtk.SeparatorMenuItem())
        menu.append(exit)

        #全局快捷键
        new.add_accelerator("activate", self.__accel_group , ord('N'), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)
        open.add_accelerator("activate", self.__accel_group , ord('O'), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)
        save.add_accelerator("activate", self.__accel_group , ord('S'), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)
        close.add_accelerator("activate", self.__accel_group , ord('W'), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)
        save_as.add_accelerator("activate", self.__accel_group , ord('S'), gtk.gdk.SHIFT_MASK|gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)

        new.connect('activate', self.on_new)
        open.connect('activate', self.on_open)
        save.connect('activate', self.on_save)
        save_as.connect('activate', self.on_save_as)
        close.connect('activate', self.on_close)
        exit.connect('activate', self.on_exit)

        return item

    def open_file_creat_new_editor(self, notebook, absname):
        file_fun.open_file_creat_new_editor(notebook, absname)

    def on_new(self, widget):
        """
        新建文件
        """
        new_editor = Pad_editor()
        file_fun.creat_new_editor(new_editor, self.__notebook)

    def on_open(self, widget):
        """
        打开文件
        """
        res, abs_filename = file_dialog.open_file_dialog(self.__mainwindow)
        if res == gtk.RESPONSE_OK:
            __path, __name = os.path.split(abs_filename)
        else:
            return
        self.open_file_creat_new_editor(self.__notebook, os.path.join(__path,__name))

    def on_save(self, widget):
        """
        保存文件
        """
        editor = self.__notebook.get_current_editor()
        if editor.is_temp():
            if editor.is_change():
                res, abs_filename = file_dialog.save_file_dialog(self.__mainwindow)
                if res == gtk.RESPONSE_OK:
                    file_fun.save_file(editor.GetText()[1], abs_filename)
                    editor.EmptyUndoBuffer()
        else:
            if editor.is_change():
                file_fun.save_file(editor.GetText()[1], editor.get_abs_filename())
                editor.EmptyUndoBuffer()

    def on_save_as(self, widget):
        """
        另存为
        """
        res, abs_filename = file_dialog.save_file_dialog(self.__mainwindow)
        if res == gtk.RESPONSE_OK:
            file_fun.save_file(editor.GetText()[1], abs_filename)
            editor.EmptyUndoBuffer()

    def on_close(self, widget):
        """
        关闭文件
        """
        editor = self.__notebook.get_current_editor()
        if editor.is_change():
            res = file_dialog.save_dialog(self.__mainwindow, editor)
            if res == gtk.RESPONSE_YES:
                if editor.is_temp():
                    res, abs_filename = file_dialog.save_file_dialog(self.__mainwindow)
                    if res == gtk.RESPONSE_OK:
                        file_fun.save_file(editor.GetText()[1], abs_filename)
                        self.__notebook.remove_page_and_editor(editor)
                else:
                    file_fun.save_file(editor.GetText()[1], editor.get_abs_filename())
                    self.__notebook.remove_page_and_editor(editor)
            elif res == gtk.RESPONSE_NO:
                self.__notebook.remove_page_and_editor(editor)
        else:
            self.__notebook.remove_page_and_editor(editor)

    def on_exit(self, widget):
        map(lambda(editor):self.on_close(widget), self.__notebook.get_editor_list())
        gtk.main_quit()